package com.yinNuo.collectWool.collectWoolHttp;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yinNuo.collectWool.core.constant.ChinaUnicomApiConstant;
import com.yinNuo.collectWool.core.constant.Costant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: yinNuo
 * create at: 2021/12/1 下午10:31
 * @description: 中国联通任务
 */
@Component
@Slf4j
public class ChinaUnicomHttps {

    /**
     * 获取响应头，得到ticket
     * @param mobile 手机号
     * @return 返回去掉ticket=的ticket字符串
     * 这里有个难题，就是使用spring自带的resttemplate 在header中存cookie并不能发送给服务器，暂时未找到原因，只能使用Apache的HttpClint或者使用hutool的http来调接口
     */
    public  String getTicket(String mobile,String ecsToken){
        String timestamp = Long.toString(DateUtil.currentSeconds());
        String url = ChinaUnicomApiConstant.GET_TICKET +
                "?to_url=https://epay.10010.com/partyServer/clockIn/index.html?channel=stsy&channelType=null&duanlianjieabc=qAPHI&time="+timestamp+"&yw_code=&desmobile="+mobile+"&version=iphone_c@8.0904";
        String header = HttpRequest.get(url)
                .header(Header.ACCEPT, "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
                .header(Header.CONNECTION, "keep-alive")
                .header(Header.HOST, "m.client.10010.com")
                .header(Header.USER_AGENT, "Mozilla/5.0 (iPhone; CPU iPhone OS 14_2_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 unicom{version:iphone_c@8.0904}{systemVersion:dis}{yw_code:}")
//                .header(Header.COOKIE, "ecs_token=eyJkYXRhIjoiNWVjMzc1MzNjZDhiYmJhZTEwYWQ1NDMzYjIyNDJkODc1YThhMjBiZmJjYWIwZDNhOTI4NTNkYTcwODBmZGRlYjc2ZDRlOTIzMGRkNTk4MDhiOGFiYTBjYTQzMjQ2MDczNGRlOGZjY2MwOTNjNjgxMzQ4ODJlOWIzYWRhOGY5MzdlNDM4MTkwMmFlNTgzYmQ3NDUyMjk1M2YxYWU0NjY4YjAxZWEzMGJiMTk5ZmIwZTUxNmJlMjI4ZDdmYjQzNGZhIiwidmVyc2lvbiI6IjAwIn0=;")//头信息，多个头信息多次调用此方法即可
                .header(Header.COOKIE, ecsToken)//头信息，多个头信息多次调用此方法即可
                .execute().header(Header.LOCATION);
        String ticket = "";
        String[] split = header.split("&");
        for (String s : split) {
            if (s.contains("ticket")) {
                ticket = s.substring(s.indexOf("=")+1);
            }

        }
        return ticket;
    }

    /**
     * 用ticket的响应头中的Location中获得ticket来获得wap_sessionID
     */
    public String getWapSessionId(String mobile,String ecsToken){
        //获得ticket
        String ticket = getTicket(mobile,ecsToken);
        Map<String, Object> params =new HashMap<>();
        params.put("activityId", "TTLXJ20210330");
        params.put("bizFrom", "stsy");
        params.put("ticket", ticket);
        params.put("type", "02");
        params.put("channelType", "null");
        String body = HttpRequest.post(ChinaUnicomApiConstant.GET_WAP_SESSIONID)
                .header(Header.ACCEPT, "application/json")
                .header(Header.ACCEPT_LANGUAGE, "zh-cn")
                .header(Header.CONNECTION, "keep-alive")
                .header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded")
                .header(Header.USER_AGENT, "Mozilla/5.0 (iPhone; CPU iPhone OS 14_2_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 unicom{version:iphone_c@8.0904}{systemVersion:dis}{yw_code:}")
                .form(params)
                .execute().body();
        JSONObject responseBody = JSONUtil.parseObj(body);
        String wap_sessionid = "";
        if (Costant.CHINA_UNICOM_CODE.equals(responseBody.getStr("returnCode"))) {
            wap_sessionid = responseBody.getStr("wap_sessionid");
        }
        return wap_sessionid;
    }



    /**
     * 中国联通天天领现金签到任务
     *
     * @return
     */
    public String chinaUnicomEveryDaySign(String phone, String ecsToken) {
        String wapSessionId = getWapSessionId(phone, ecsToken);
        Map<String, String> headers =new HashMap<>();
        headers.put("Accept", "application/json");
        headers.put("Accept-Language", "zh-cn");
        headers.put("Connection", "keep-alive");
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        headers.put("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 14_2_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 unicom{version:iphone_c@8.0904}{systemVersion:dis}{yw_code:}");

        Map<String, Object> params =new HashMap<>();
        params.put("loginId", phone);
        params.put("activityId", "TTLXJ20210330");
        params.put("wap_sessionID", wapSessionId);
        params.put("version", "3.0.0");
//        params.put("drawType", "B");
        //{"returnCode":"0","amount":"1.86","returnMsg":"ok","prizeId":"ZFGJBXJJL4","winMsg":null}
        /*String body = HttpRequest.post(ChinaUnicomApiConstant.EVERY_DAY_SIGN)
                .addHeaders(headers).form(params)
                .execute().body();*/
        String body = gassDrawType(headers, params);
        return body;
    }
    public String gassDrawType(Map<String, String> headers,Map<String, Object> params) {
        String[] drawTypes = {"A", "B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
        String body = "";
        for (String drawType : drawTypes) {
            params.put("drawType", drawType);
            body = HttpRequest.post(ChinaUnicomApiConstant.EVERY_DAY_SIGN)
                    .addHeaders(headers).form(params)
                    .execute().body();
            log.info("body是{}",body);
            JSONObject jsonObject = JSONUtil.parseObj(body);
            //返回码是0则获取成功
            if (Costant.CHINA_UNICOM_CODE.equals(jsonObject.getStr("returnCode"))) {
                return body;
            }
        }
        return body;
    }

}
